# -*- coding: utf-8 -*-

import os
import cgi
import urllib
import sys
import md5
import time

from django.utils import simplejson
from xml.dom import minidom

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import mail
from google.appengine.api import urlfetch
from google.appengine.api.urlfetch_errors import *
from google.appengine.api import users
from google.appengine.ext.db import Key
from google.appengine.ext.db import BadKeyError
from google.appengine.ext.webapp import template
from time import strftime

from alisoft import AliSoftAPI

from baseclass import BaseHandler

class ProductQuery(BaseHandler):
    def get(self):
        template_values = {}
        self.outputTemplate(template_values, "product_query_view")        
    
    def post(self):
        api = AliSoftAPI()
        req = api.getReq('taobao.products.search')
        req['v'] = '1.0'
        req['fields'] = "product_id,name"        
        req['format'] = 'json'
        req['q'] = self.request.get('querykey')
        result = api.reqAndRspPlain(req)
        template_values = {'info': str(result.headers.items())+"<hr>"+result.content}
        self.outputTemplate(template_values, "product_query_view")
        # TODO: leave here 2009-03-23
        

application = webapp.WSGIApplication([('/product/query', ProductQuery)],
                                     debug=True)
    
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
  main()